export default function useWeeklyRanges(options = {}) {
    const {
        startYear = 2023,
        startMonth = 1,
        endDate = new Date(),
    } = options

    // Helper function to get the Monday of a given week
    const getMonday = (date) => {
        const d = new Date(date)
        const day = d.getDay() // 0 (Sunday) to 6 (Saturday)
        const diff = d.getDate() - day + (day === 0 ? -6 : 1) // Adjust to Monday
        return new Date(d.setDate(diff))
    }

    // Format date as YYYY-MM-DD
    const formatDate = (date) => {
        const pad = num => num.toString().padStart(2, '0')
        return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
    }

    // Format date as YYYYMMDD (no separators)
    const formatDateCompact = (date) => {
        const pad = num => num.toString().padStart(2, '0')
        return `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}`
    }

    // Calculate ISO week number (Monday-based)
    const getWeekNumber = (date) => {
        const d = new Date(date)
        d.setHours(0, 0, 0, 0)
        // Thursday in current week decides the year
        d.setDate(d.getDate() + 3 - (d.getDay() || 7) + 1)
        // January 4 is always in week 1
        const week1 = new Date(d.getFullYear(), 0, 4)
        // Adjust to Thursday in week 1 and count number of weeks
        return 1 + Math.round(((d - week1) / 86400000 - 3 + (week1.getDay() || 7)) / 7)
    }

    // Main computation for weekly ranges
    const weeks = computed(() => {
        const result = []
        const currentMonday = getMonday(new Date(startYear, startMonth - 1, 1))
        const end = new Date(endDate)

        // Generate weekly ranges
        while (currentMonday <= end) {
            const weekStart = new Date(currentMonday)
            const weekEnd = new Date(currentMonday)
            weekEnd.setDate(weekEnd.getDate() + 6)

            result.push({
                label: `${formatDate(weekStart)} ~ ${formatDate(weekEnd)}`,
                start: formatDate(weekStart),
                end: formatDate(weekEnd),
                value: `${formatDateCompact(weekStart)}-${formatDateCompact(weekEnd)}`,
                weekNumber: getWeekNumber(weekStart),
                year: weekStart.getFullYear(),
            })

            // Move to next Monday
            currentMonday.setDate(currentMonday.getDate() + 7)
        }
        const weekRangs = result
        return weekRangs.slice(0, weekRangs.length - 1).reverse()
    })

    // Options for select/dropdown components
    const weekOptions = computed(() =>
        weeks.value.map(w => ({
            label: `Week ${w.weekNumber}, ${w.year} (${w.start} - ${w.end})`,
            value: w.value,
            weekNumber: w.weekNumber,
            year: w.year,
        })),
    )

    // Current week information (Monday-based)
    const currentWeek = computed(() => {
        const monday = getMonday(new Date())
        const weekEnd = new Date(monday)
        weekEnd.setDate(weekEnd.getDate() + 6)

        return {
            label: `Week ${getWeekNumber(monday)} (${formatDate(monday)} - ${formatDate(weekEnd)})`,
            value: `${formatDateCompact(monday)}-${formatDateCompact(weekEnd)}`,
            weekNumber: getWeekNumber(monday),
            year: monday.getFullYear(),
            start: formatDate(monday),
            end: formatDate(weekEnd),
        }
    })

    return { weeks, weekOptions, currentWeek }
}
